/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.system;

import java.io.Serializable;
import java.util.Iterator;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

/**
 *
 * @author chan
 */
@ManagedBean
@SessionScoped
public class UtilityBean implements Serializable{
    private String ClientId;
    
    
    
    
    public String cid(UIComponent component) {
        FacesContext context = FacesContext.getCurrentInstance();
        return component.getClientId(context);
      }
    
     public String getClientId() {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot root = context.getViewRoot();
        context.getExternalContext().getRequestParameterMap().get("componentId");
        final String componentId = context.getExternalContext().getRequestParameterMap().get("componentId");
        UIComponent c = findComponent(root, componentId);
        ClientId = c.getClientId(context);
        return ClientId;
      }

  /**
   * Finds component with the given id
   */
  private UIComponent findComponent(UIComponent c, String id) {
    if (id.equals(c.getId())) {
      return c;
    }
    Iterator<UIComponent> kids = c.getFacetsAndChildren();
    while (kids.hasNext()) {
      UIComponent found = findComponent(kids.next(), id);
      if (found != null) {
        return found;
      }
    }
    return null;
  }
  
  public String findClientId(String clientId) {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot root = context.getViewRoot();

        final String componentId = clientId;
        UIComponent c = findComponent(root, componentId);
        return c.getClientId(context);
    }
    
    
}
